package UI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import utils.FichierTexte;

import MainLaunch.ClassLoaderException;
import MainLaunch.LoaderClass;
import MainLaunch.StaticFields;
import VideoTools.AnalyseListOfCircle;
import VideoTools.Movement;
import VideoTools.ThreadVideo;
import VideoTools.Movement.ModeSimulation;
import VideoTools.ThreadVideo.Mode;



public class Window extends JFrame implements ActionListener{

	private JButton startStop;
	
	// menu
	private JMenuBar barMenu;
	private JMenu menu1;
	private JMenuItem menu1Item1;
	private JMenuItem menu1Item2;
	public JMenuItem menu1Item3;
	private JMenu menu2;
	private JMenu menu2Item1;
	private JCheckBoxMenuItem menu2Item1Item1;
	private JCheckBoxMenuItem menu2Item1Item2;
	private JMenu menu2Item2;
	private JRadioButtonMenuItem[] menu2Item2Item = new JRadioButtonMenuItem[AnalyseListOfCircle.AnalyseAlgorythme.values().length];
	private JMenuItem menu2Item3;
	private JMenu menu2Item4;
	private JMenuItem menu2Item4Item1;
	private JMenuItem menu2Item4Item2;
	private JMenuItem menu2Item4Item3;
	private JMenuItem menu2Item4Item4;
	private JMenuItem menu2Item4Item5;
	private JMenuItem menu2Item4Item6;
	private JMenu menu3;
	private JMenuItem menu3Item1;
	private JMenuItem menu3Item2;
	
	public static Dimension dimensionScreen;

	

	public Window() {
		createMenuBar();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		startStop = new JButton("Stop");
		this.add(startStop,BorderLayout.SOUTH);
		this.setTitle("Webcam API");
		this.setVisible(true);
		startStop.addActionListener(this);
		//		 adding action listener
		menu1Item1.addActionListener(this);
		menu1Item2.addActionListener(this);
		menu1Item3.addActionListener(this);
		menu2Item1Item1.addActionListener(this);
		menu2Item1Item2.addActionListener(this);
		for (AnalyseListOfCircle.AnalyseAlgorythme a: AnalyseListOfCircle.AnalyseAlgorythme.values()){
			menu2Item2Item[a.ordinal()].addActionListener(this);
		}
		menu2Item3.addActionListener(this);
		menu3Item1.addActionListener(this);
		menu3Item2.addActionListener(this);
		menu2Item4Item1.addActionListener(this);
		menu2Item4Item2.addActionListener(this);
		menu2Item4Item3.addActionListener(this);
		menu2Item4Item4.addActionListener(this);
		menu2Item4Item5.addActionListener(this);
		menu2Item4Item6.addActionListener(this);
		
		/** size of the screen */
		dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	}
	
	/**
	 * add the menu bar to the windows 
	 * add the listener for all the menuItem
	 */
	private void createMenuBar(){
		// menu1 
		menu1 = new JMenu("Start");
		menu1Item1 = new JMenuItem("Init");
		menu1Item2 = new JMenuItem("Load");
		menu1Item3 = new JMenuItem("PlayQuickGame");
		menu1.add(menu1Item1);
		menu1.add(menu1Item2);
		menu1.add(menu1Item3);
		// menu2
		menu2 = new JMenu("Settings");
		// item1 of menu2
		menu2Item1 = new JMenu("Substitution");
		menu2Item1Item1 = new JCheckBoxMenuItem("Mouse");
		menu2Item1Item2 = new JCheckBoxMenuItem("Keyboard",true);
		menu2Item1.add(menu2Item1Item1);
		menu2Item1.add(menu2Item1Item2);
		//item2 of menu2
		menu2Item2 = new JMenu("Movement Detection");
		for (AnalyseListOfCircle.AnalyseAlgorythme a: AnalyseListOfCircle.AnalyseAlgorythme.values()){
			menu2Item2Item[a.ordinal()] = new JRadioButtonMenuItem(a.name());
			menu2Item2.add(menu2Item2Item[a.ordinal()]);
		}
		menu2Item2Item[AnalyseListOfCircle.algo.ordinal()].setSelected(true);
		menu2Item3 = new JMenuItem("Settings");
		menu2Item4 = new JMenu("Nombre de balles :");
		menu2Item4Item1 = new JMenuItem("1");
		menu2Item4Item2 = new JMenuItem("2");
		menu2Item4Item3 = new JMenuItem("3");
		menu2Item4Item4 = new JMenuItem("4");
		menu2Item4Item5 = new JMenuItem("5");
		menu2Item4Item6 = new JMenuItem("6");
		menu2Item4.add(menu2Item4Item1);
		menu2Item4.add(menu2Item4Item2);
		menu2Item4.add(menu2Item4Item3);
		menu2Item4.add(menu2Item4Item4);
		menu2Item4.add(menu2Item4Item5);
		menu2Item4.add(menu2Item4Item6);
		menu2.add(menu2Item1);
		menu2.add(menu2Item2);
		menu2.add(menu2Item4);
		menu2.add(menu2Item3);
		
		// menu3
		menu3 = new JMenu("?");
		menu3Item1 = new JMenuItem("Help");
		menu3Item2 = new JMenuItem("About");
		menu3.add(menu3Item1);
		menu3.add(menu3Item2);
		// bar
		barMenu=new JMenuBar();
		barMenu.add(menu1);
		barMenu.add(menu2);
		barMenu.add(menu3);
		// adding to the JFrame
		this.setJMenuBar(barMenu);
		
	}

	public void actionPerformed(ActionEvent arg0)  {
		if (arg0.getSource()==startStop){
			System.out.println("stopstart");
			if (startStop.getText().equals("Start")){
				startStop.setText("Stop");
			}
			else startStop.setText("Start");
			ThreadVideo.stopUnstopped();
		}

		//		 init
		if (arg0.getSource()==menu1Item1){
			System.out.println("initialising");
			ThreadVideo.initialisation();
		}
		
		// load
		if(arg0.getSource()==menu1Item2){
			System.out.println("load");
			startStop.doClick();
			new LoadGame(this,new FichierTexte(StaticFields.ressourcePath + "History.txt"));
		}
		
		//	playquickgame
		if(arg0.getSource()==menu1Item3){
			System.out.println("quickgame");
			startStop.doClick();
			new LoadGame(this,new FichierTexte(StaticFields.ressourcePath + "Default.txt"));
		}
		
		// analyse
		for (AnalyseListOfCircle.AnalyseAlgorythme a: AnalyseListOfCircle.AnalyseAlgorythme.values()){
			menu2Item2Item[a.ordinal()].setSelected(false);
			if (arg0.getSource()==menu2Item2Item[a.ordinal()]){
				System.out.println(a.name());
				menu2Item2Item[a.ordinal()].setSelected(true);
				AnalyseListOfCircle.algo=a;
			}
			
		}
		
			
		// Mouse
		if (arg0.getSource()==menu2Item1Item1){
			System.out.println("Mouse");
			menu2Item1Item1.setSelected(menu2Item1Item1.isSelected());
			if (menu2Item1Item1.isSelected()){
				if (menu2Item1Item2.isSelected()){
					Movement.mode=ModeSimulation.BOTH;
				}
				else Movement.mode=ModeSimulation.MOUSE;
			}
			else {
				if (menu2Item1Item2.isSelected()){
					Movement.mode=ModeSimulation.KEYBOARD;
				}
				else Movement.mode=ModeSimulation.NONE;
			}
			System.out.println(Movement.mode);
		}
		
		// Keyboard
		if (arg0.getSource()==menu2Item1Item2){
			System.out.println("Keyboard");
			menu2Item1Item2.setSelected(menu2Item1Item2.isSelected());
			
			if (menu2Item1Item2.isSelected()){
				if (menu2Item1Item1.isSelected()){
					Movement.mode=ModeSimulation.BOTH;
				}
				else Movement.mode=ModeSimulation.KEYBOARD;
			}
			else {
				if (menu2Item1Item1.isSelected()){
					Movement.mode=ModeSimulation.MOUSE;
				}
				else Movement.mode=ModeSimulation.NONE;
			}
			System.out.println(Movement.mode);
		}
		
		//	settings
		if (arg0.getSource()==menu2Item3){
			new Settings();
		}
		
		// help
		if (arg0.getSource()==menu3Item1){
			new Aide();
		}
		
		// about
		if (arg0.getSource()==menu3Item2){
			new Apropos();
		}
		
		// changement du nombre de point
		if (arg0.getSource()==menu2Item4Item1){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item1.getText()));
		}
		if (arg0.getSource()==menu2Item4Item2){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item2.getText()));
		}
		if (arg0.getSource()==menu2Item4Item3){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item3.getText()));
		}
		if (arg0.getSource()==menu2Item4Item4){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item4.getText()));
		}
		if (arg0.getSource()==menu2Item4Item5){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item5.getText()));
		}
		if (arg0.getSource()==menu2Item4Item6){
			ThreadVideo.changeNumberOfPoint(Integer.parseInt(menu2Item4Item6.getText()));
		}
	}

	
	public void LoadAClassWithHisNameFile(File file){
		if (file!=null){
			// load the file with classloader
			try {
				LoaderClass Lc= new LoaderClass(file);
				Lc.process();
				startStop.doClick();
			} catch (ClassLoaderException e) {
				System.out.println("erreur de class loader" + e.getMessage());
			}
		}
	}
	

	


}
